package com.android.readcard.utils.djlm;

import android.util.Log;

import com.android.readcard.server.CommandItem;
import com.android.readcard.utils.djlm.DJLMReader3.reader.HexUtil;

import java.util.Arrays;

/**
 * Created by Administrator on 2017/3/10.
 */
//临时缓存及过滤
public class TempCache {
    public byte[] FindCard = new byte[]{0x05, 0x00, 0x00};
    public byte[] SelectCard = new byte[]{0x1D, 0x00, 0x00, 0x00, 0x00, 0x00,0x08,0x01, 0x08};
    protected byte[] GET_CHALLENGE = new byte[]{0x00,(byte)0x84, 0x00, 0x00, 0x08};
    ////////////////////////////////////////////////////////
    public byte[] SelectFile6002 = new byte[]{0x00, (byte)0xA4, 0x00, 0x00, 0x02, 0x60, 0x02};
    protected byte[] SelectFile6011 = new byte[]{0x00, (byte)0xA4, 0x00, 0x00, 0x02, 0x60, 0x11};
    protected byte[] EXTERNAL_AUTHENTICATE = new byte[]{0x00, (byte)0x82, 0x00, 0x52, 0x0A, (byte)0xF0, 0x01};
    public byte[] ReadBinary0Query = new byte[]{(byte)0x80, (byte)0xB0, 0x00,0x00, 0x10};
    ////////////////////////////////////////////////////////
    public byte[] ReadNullResponse = new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte)0x90, 0x00};
    protected byte[] FindCardRet = new byte[]{0x50, 0x00, 0x00, 0x00, 0x00, (byte)0xD1, 0x03, 0x00, (byte)0x81, 0x00, 0x70, (byte)0xC0};
    public byte[] CardErrRet = new byte[]{0x6D, 0x00};
    protected byte[] SelectCardRet = new byte[]{0x08};
    protected byte[] OK_ret = new byte[]{(byte)0x90, 0x00};
    /////////////////////////////////////////
    private byte [] m_LastQuery;
    private byte [] m_lastResponse;
    boolean m_nSZ = false;
    static void LogOut(String strTest)
    {
        Log.i("TempCache", strTest);
        //System.out.println(strTest);
    }

    public TempCache()
    {

    }

    public void ConverToSZ()
    {
        FindCard = CommandItem.QueryBytesToStrig(FindCard, FindCard.length);
        SelectCard = CommandItem.QueryBytesToStrig(SelectCard, SelectCard.length);
        GET_CHALLENGE = CommandItem.QueryBytesToStrig(GET_CHALLENGE, GET_CHALLENGE.length);
        ////////////////////////////////////////////////////////
        SelectFile6002 =  CommandItem.QueryBytesToStrig(SelectFile6002, SelectFile6002.length);
        SelectFile6011 =  CommandItem.QueryBytesToStrig(SelectFile6011, SelectFile6011.length);
        EXTERNAL_AUTHENTICATE =  CommandItem.QueryBytesToStrig(EXTERNAL_AUTHENTICATE, EXTERNAL_AUTHENTICATE.length);
        ReadBinary0Query =  CommandItem.QueryBytesToStrig(ReadBinary0Query, ReadBinary0Query.length);
        ////////////////////////////////////////////////////////
        ReadNullResponse =  CommandItem.ResponseBytesToString(ReadNullResponse, ReadNullResponse.length);
        ///////////////////////////////////////////////////////
        FindCardRet =  CommandItem.ResponseBytesToString(FindCardRet, FindCardRet.length);
        CardErrRet =  CommandItem.ResponseBytesToString(CardErrRet, CardErrRet.length);
        SelectCardRet = CommandItem.ResponseBytesToString(SelectCardRet, SelectCardRet.length);
        OK_ret = CommandItem.ResponseBytesToString(OK_ret, OK_ret.length);
        m_nSZ = true;
    }

    void ResetCache()
    {
        m_LastQuery = null;
        m_lastResponse = null;
        LogOut("ResetCache!");
    }

    byte[] FilterSameQuery(byte[] Query) {
        if (Query != null) {
            if (Arrays.equals(Query, m_LastQuery) && m_lastResponse != null && !Arrays.equals(Query, FindCard)) {
                LogOut("ble short!" + HexUtil.bytesToHexString(m_lastResponse));
                return m_lastResponse;
            }
        }
        return null;
    }

    public void RecordQueryRespone(byte[] Query, byte[] Respone)
    {
        if (Query != null && Respone != null) {
            if (!Arrays.equals(m_LastQuery, Query) || !Arrays.equals(m_lastResponse, Respone))
                m_LastQuery = Query;
            m_lastResponse = Respone;
            //LogOut("RecordQueryRespone");
        }
    }

    public byte[] FilterCrossResponse(byte[] Query, byte[] Respone)
    {
        if (Respone != null && Query != null)
        {
            if (m_LastQuery != null && m_lastResponse != null) {
                if (Arrays.equals(Respone, m_lastResponse) && !Arrays.equals(Query, m_LastQuery)) {
                    if (Arrays.equals(Respone, CardErrRet))
                    {
                        if(!Arrays.equals(Query, FindCard) && !Arrays.equals(Query, SelectCard)) {
                            LogOut("Cross filter! (6D00)" + HexUtil.bytesToHexString(Respone));
                            return null;
                        }
                    }
                    else if (Arrays.equals(Respone, OK_ret)) {
                        if (!Arrays.equals(Query, SelectFile6011)) {
                            LogOut("Cross filter!(9000)" + HexUtil.bytesToHexString(Respone));
                            return null;
                        }
                    }
                    else
                    {
                        if (!Arrays.equals(Query, FindCard))
                        {
                            if (m_nSZ)
                                LogOut("Cross filter! String " + new String(Respone));
                            else
                                LogOut("Cross filter!" + HexUtil.bytesToHexString(Respone));
                            return null;
                        }
                    }
                }
            }
        }
        return Respone;
    }

    public byte[] FixResponse(byte[] Query, byte[] Respone)
    {
        if (Query != null && Respone != null) {

            if (Arrays.equals(Respone, CardErrRet)) {
                if (Arrays.equals(Query, FindCard)) {
                    LogOut("fix fidncard!");
                    return FindCardRet;
                }
            }

            if (Arrays.equals(Respone, CardErrRet)) {
                if (Arrays.equals(Query, SelectCard)) {
                    LogOut("fix select!");
                    return SelectCardRet;
                }
            }
            if (Query.length == 15 && Respone != null
                    && Arrays.equals(Arrays.copyOf(Query, 7), EXTERNAL_AUTHENTICATE) && !Arrays.equals(Respone, OK_ret)) {
                LogOut("fix EXTERNAL AUTHENTICATE!");
                return OK_ret;
            }
        }
        return Respone;
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////////
}
